﻿/**
 * $HeadURL: http://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.WebUI/SmartCollector.WebUI/Models/ItemRepository.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-05-05 19:05:31 +0000 (Thu, 05 May 2011) $
 * $Rev: 115 $
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SmartCollector.WebUI.Models;

namespace SmartCollector.WebUI.Models
{
    public class ItemRepository
    {
        TotalNewsDataContext tn = new TotalNewsDataContext();
        /// <summary>
        /// Lay category cha
        /// </summary>
        /// <returns></returns>
        public IQueryable<tbl_Category> GetCategories(int num)
        {
            var c = from i in tn.tbl_Categories
                    where i.ParentID == null & i.ID!=num
                    select i;
            return c;
        }
        /// <summary>
        /// Lay category con
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public IQueryable<tbl_Category> GetCategoriesById(int? ID)
        {
            var c = from i in tn.tbl_Categories
                    where i.ParentID == ID 
                    select i;
            return c;
        }
        /// <summary>
        /// Lay tin theo category cha va con 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public IQueryable<tbl_Category> GetAllCategoriesById(int? cateId)
        {
            var c = from i in tn.tbl_Categories
                    where i.ParentID == cateId || i.ID==cateId 
                    select i;
            return c;
        }
        public tbl_Category GetSingleCategoriesById(int? cateId)
        {
            var c = from i in tn.tbl_Categories
                    where i.ParentID == cateId || i.ID == cateId
                    select i;
            return c.First();
        }
        public tbl_Item GetItemById(int id)
        {
            //return tn.tbl_Items.SingleOrDefault(i => i.ID == id);
            var t = (from i in tn.tbl_Items
                     where i.Id == id
                     select i).Single();
            t.TotalView = t.TotalView + 1;
            Save();
            return t;
          
        }
        /// <summary>
        /// Lay 1 tin moi nhat theo subcateId
        /// </summary>
        /// <param name="id">cateId</param>
        /// <returns>i</returns>
        public IQueryable<tbl_Item> GetOneItemByCateId(int? cateId)
        {
            var i = from it in tn.tbl_Items
                    where it.CategoryID == cateId
                    orderby it.Date descending
                    select it;
            return i.Take(1);
        }
        /// <summary>
        /// Lay 3 tin theo subcateId sau tin moi nhat
        /// </summary>
        /// <param name="cateId">cateId</param>
        /// <returns>3 tin</returns>
        public IQueryable<tbl_Item> GetItemAfterNewsByCateId(int? cateId)
        {
            DateTime dt= DateTime.Now;

            foreach (var i in GetOneItemByCateId(cateId))
            {
                dt = i.Date;
            }
            var item = from it in tn.tbl_Items
                       where it.CategoryID == cateId & it.Date < dt
                       orderby it.Date descending
                       select it;
            return item.Take(3);
        }
        /// <summary>
        /// Lay 1 tin moi nhat cua cate cha theo cateid
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public IQueryable<tbl_Item> GetNewItemByCateId(int? cateId)
        {
            var t = (from i in tn.tbl_Items
                     where i.CategoryID == cateId
                     orderby i.Date descending
                     select i).Take(1);
            return t;
        }
        /// <summary>
        /// Lay danh sach tin moi nhat theo categoryId
        /// </summary>
        /// <returns></returns>
        public IQueryable<tbl_Item> GetItemFromCategory(int? id)
        {
            //return tn.tbl_Items.Where(p => p.CategoryID == id).ToList();
            DateTime d = new DateTime();
            foreach (var item in GetNewItemByCateId(id))
            {
                d = item.Date;
            }
            var c = from e in tn.tbl_Items
                    where e.CategoryID == id & e.Date<d
                    orderby e.Date descending
                    select e;
            return c;
        }
        /// <summary>
        /// Lay tin dau tien trong tbl_Item
        /// </summary>
        /// <returns></returns>
        public IQueryable< tbl_Item> GetOneItem()
        {
            return  (from i in tn.tbl_Items
                     where i.ImageUrl!=string.Empty
                    orderby i.Date descending
                    select i).Take(1);
        }
        /// <summary>
        /// Lay tin moi nhat sau tin dau trong tbl_Item (trang chu)
        /// </summary>
        /// <returns>Danh sach tin</returns>
        public IEnumerable<tbl_Item> GetTopNewsAfterOne()
        {
            DateTime dt= DateTime.Now;
            foreach (var item in GetOneItem())
	        {
                dt= item.Date;
	        }
            var a = from t in tn.tbl_Items
                    where t.Date < dt
                    orderby t.Date descending
                    select t;
            return a.Take(8).ToList();
        }
        public void Save()
        {
            tn.SubmitChanges();
        }
        /// <summary>
        /// Lay tin xem nhieu nhat trong tbl_item
        /// </summary>
        /// <returns></returns>
        public IQueryable<tbl_Item> GetTindocnhieu()
        {
            return tn.tbl_Items.Where(i => i.TotalView > 0).OrderByDescending(t=>t.TotalView).Take(5);
        }
        /// <summary>
        /// Lay tin xem nhieu nhat trong tbl_Item theo cateId
        /// </summary>
        /// <param name="cateId">cateId</param>
        /// <returns></returns>
        public IQueryable<tbl_Item> GetTindocnhieuByCateId(int cateId)
        {
            return tn.tbl_Items.Where(i => i.TotalView > 0 & i.CategoryID==cateId).OrderByDescending(t => t.TotalView).Take(1);
        }
        public IQueryable<tbl_Item> GettinlienquanByCateId(int cateId)
        {
            var t = from i in tn.tbl_Items
                    where i.CategoryID == cateId
                    select i;
            return t.Take(10);
        }
        //public IList<tbl_Item> GetTintieudiem()
        //{
        //   List<tbl_Item> list = new List<tbl_Item>();
        //   foreach (var item in GetCategories(1))
        //   {
        //       var c = (from i in tn.tbl_Items
        //                where i.CategoryID == item.ID
        //                orderby i.Date descending
        //                select i).FirstOrDefault();
        //           list.Add(c);
        //   }
        //   return list;
        //}
    }
}